import sys
import traceback

from gen_load_content_to_csv import load_data_from_db, save_to_db
from gen_template import word_replace, tran2, tran

"""
    生成模板入口
"""
if __name__ == '__main__':
    try:
        resdir = "."

        test_cvs = "test.csv"
        # test_cvs = "ctx20220321-2.csv"
        # 读数据库保存到CSV
        load_data_from_db(resdir, test_cvs)
        rs1_txt = "rs1.txt"
        # 第一轮生成模板
        tran(".", test_cvs, rs1_txt)
        sort_txt = "rs2_sort.txt"
        # 排序并去重
        word_replace(".", rs1_txt, sort_txt)
        rs3_txt = "rs3.txt"
        # 第二轮生成模板
        tran2(".", sort_txt, rs3_txt)
        # 入库
        save_to_db(resdir, rs3_txt)
    except Exception as e:
        print(e)
        traceback.print_exc(file=sys.stdout)
